package com.qf.device.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.core.dto.PageDto;
import com.qf.core.vo.PageResult;
import com.qf.core.vo.R;
import com.qf.device.dto.DeviceDto;
import com.qf.device.entity.Device;
import com.qf.device.mapper.DeviceMapper;
import com.qf.device.service.DeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.product.entity.Product;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2023-10-26
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {

    @Override
    public PageResult<Device> findPage(PageDto<DeviceDto> deviceDto) {
        IPage<Device> page = new Page<>(deviceDto.getNow(), deviceDto.getSize());
        IPage<Device> pageInfo = this.baseMapper.selectDevice(page, deviceDto.getWhere());
        PageResult<Device> pageResult = new PageResult<>();
        pageResult.setPages(pageInfo.getPages());
        pageResult.setList(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public R selectActive() {
        List<Device> devices = this.baseMapper.selectList(null);
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (Device device : devices) {
            arrayList.add(device.getActive());
        }
        Set<Integer> set = new HashSet<>();
        set.addAll(arrayList);

        return R.ok(set,"查询成功");
    }

    @Override
    public R deleteDevice(String id) {
        int i = this.baseMapper.deleteById(id);
        if (i>0){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
}
